//class Solution {
//public:
//    vector<vector<string>> groupAnagrams(vector<string>& strs) {
//        map<string, vector<string>> mp;
//        for (string& str : strs) {
//            string key = str;
//            sort(key.begin(), key.end());
//            mp[key].push_back(str);
//        }
//        vector<vector<string>> ans;
//        for (auto it = mp.begin(); it != mp.end(); it++) {
//            ans.push_back(it->second);
//        }
//        return ans;
//    }
//};
//
#include<iostream>
#include<set>
#include<vector>
using namespace std;

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        set<int> se;
        for (auto e : nums) {
            se.insert(e);
        }
        int ans = 0;
        set<int>::iterator it = se.begin();
        vector<int> vt;
        while (it != se.end()) {
            vt.push_back(*it);
            ++it;
        }
        int l = 0, r = 0;
        while (r < vt.size() && l < vt.size()) {
            if (r + 1 < vt.size() && vt[r] + 1 == vt[r + 1]) {
                ++r;
            }
            else {
                ans = max(ans, r - l + 1);
                l = r + 1;
                r = l;
            }
        }
        return ans;
    }
};

int main()
{
    vector<int> vrem({ 100,4,200,1,3,2 });
    cout << Solution().longestConsecutive(vrem) << endl;
    return 0;
}

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        set<int> se;
        for (auto e : nums) {
            se.insert(e);
        }
        int ans = 0;
        set<int>::iterator it = se.begin();
        vector<int> vt;
        vt.reserve(10000);
        while (it != se.end()) {
            vt.push_back(*it);
            ++it;
        }
        int l = 0, r = 0;
        while (r < vt.size() && l < vt.size()) {
            if (r + 1 < vt.size() && vt[r] + 1 == vt[r + 1]) {
                ++r;
            }
            else {
                ans = max(ans, r - l + 1);
                l = r + 1;
                r = l;
            }
        }
        return ans;
    }
};

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> num_set;
        for (const int& num : nums) {
            num_set.insert(num);
        }
        int ans = 0;
        for (const int num : num_set) {
            if (!num_set.count(num - 1)) {
                int currentNum = num;
                int currentAns = 1;
                while (num_set.count(currentNum + 1)) {
                    ++currentNum;
                    ++currentAns;
                }
                ans = max(currentAns, ans);
            }
        }
        return ans;
    }
};